#pragma once

#include "Socket.hpp"
#include "common.hpp"
#include <sys/epoll.h>


using namespace SocketModule;

class Reactor; // 前置声明
class Connection; // 前置声明


class Connection
{
public:
    static const int defalut_error_buff_size = 1024;
    static const uint32_t defalut_event = (EPOLLIN | EPOLLET);

    static const uint16_t default_dist_port = 0; // 默认对端主机port，表示任意/未初始化
    static const uint32_t default_dist_ip = 0;   // 默认对端主机ip，表示任意/未初始化

    Connection(Reactor *owner,uint16_t dist_port=default_dist_port,uint32_t dist_ip=default_dist_ip)
        : _event(defalut_event),
          _owner(owner),
          _dist_port(dist_port),
          _dist_ip(dist_ip),
          _timestamp(time(nullptr))//获取当前时间戳
    {
    }
    uint32_t Event() const
    {
        return _event;
    }
    void SetEvent(uint32_t event)
    {
        _event = event;
    }
    void AppendsReadbuff(const string &in)
    {
        _readbuff += in;
    }
    void AppendsWritebuff(const string &out)
    {
        _writebuff += out;
    }
    void RemoveWritebuff(uint32_t n) // 删除前n个字节的数据
    {
        _writebuff.erase(0, n);
    }

    string &Readbuff()
    {
        return _readbuff;
    }
    Reactor* Owner()
    {
        return _owner;
    }

    void UpdateTimestamp()
    {
        _timestamp=time(nullptr);//获取当前时间戳
    }

    uint64_t Timestamp()
    {
        return _timestamp;
    }

    uint16_t DistPort()
    {
        return _dist_port;
    }
    uint32_t DistIp()
    {
        return _dist_ip;
    }

    const string &Writebuff()
    {
        return _writebuff;
    }



    virtual int Fd() = 0;
    virtual void Recver() = 0;
    virtual void Sender() = 0;
    virtual void Excepter() = 0;

    virtual ~Connection()
    {
    }

protected:
    uint16_t _dist_port; // 对端主机的port
    uint32_t _dist_ip;   // 对端主机的ip
    uint32_t _event;     // 该连接关心的事件
    string _readbuff;    // 每个连接一个，用户级接收缓冲区
    string _writebuff;   // 每个连接一个，用户级发送缓冲区
    Reactor *_owner;     // 所属的Reactor
    uint64_t _timestamp; //用于进行连接的超时管理
};
